/* 
  库存
 */
<template>
  <yd-layout>
    <!-- <yd-navbar slot="navbar" title="库存" bgcolor="#0a0a0a" color="#fff" /> -->
    <yd-infinitescroll :callback="loadmore" ref="infinitescroll">
      <div slot="list" style="padding-top:5px;">
        <stock-item 
          v-for="(item, i) in list" :key="i"
          :title="item.pname"
          :count="item.count"
          :use-spec="item.count"
          :surplus-spec="item.surplusSpec"
          :spec="item.spec"
          @click.native.stop="handleToStaffStock(item)"
         />
      </div>
      <img slot="loadingTip" src="http://static.ydcss.com/uploads/ydui/loading/loading10.svg"/>
      <span slot="doneTip">无数据</span>
    </yd-infinitescroll>
    <home-tabbar slot="tabbar" selected="stock"></home-tabbar>
  </yd-layout>
</template>
 
<script>
import pageMixin from "../../../mixins/page";
import HomeTabbar from "./home-tabbar";
import StockItem from "./Stock/stock-list-item";
export default {
  mixins: [pageMixin],
  components: {
    HomeTabbar,
    StockItem,
  },
  data() {
    return {
      pagination: {
        rows: 10,
        page: 1,
      },
      list: [],
    };
  },
  computed: {
    uid() {
      return this.$store.state.uid;
    },
  },
  methods: {
    async handleRefresh() {
      await this.loadData();
      this.$refs["pullrefresh"].$emit("ydui.pullrefresh.finishLoad");
    },
    // 跳转至领用记录
    handleToStaffStock(item) {
      this.$router.push(`/beautician/stock/records/${item.pid}`)
    },
    handleSubmitReport() {
      let rids = this.rids;
      if (rids.length === 0) {
        this.$dialog.toast({
          mes: "请选择",
          timeout: 1000,
        });
        return;
      }

      this.$dialog.confirm({
        title: "提示",
        mes: "是否确认上报？",
        opts: async () => {
          this.indicator = true;
          await this.upReport(rids).catch(err => {});
          await this.loadData();
          this.indicator = false;
        },
      });
    },
    async loadmore() {
      let res = await this.loadData().catch(err => {});

      if (res.rows.length < this.pagination.rows) {
        this.$refs["infinitescroll"].$emit("ydui.infinitescroll.loadedDone");
      } else {
        this.$refs["infinitescroll"].$emit("ydui.infinitescroll.finishLoad");
      }
    },
    async loadData() {
      return new Promise(async (resolve, reject) => {
        let res = await this.$api
          .searchStocks({
            fid: this.uid,
            rows: this.pagination.rows,
            page: this.pagination.page,
          })
          .catch(err => {
            reject(err);
          });

        if (res) {
          this.total = res.total;
          this.list = this.list.concat(res.rows);
          resolve(res);
        } else {
          reject();
        }
      });
    },
    async init() {
      this.indicator = true;
      await this.loadData();
      this.indicator = false;
    },
  },
  created() {
    this.init();
  },
};
</script>
 
<style scoped>
.b-info {
  background-color: #fff;
  padding: 10px;
  font-size: 12px;
  color: #333;
  border-top: 1px solid #eee;
  border-bottom: 1px solid #eee;
}
</style>
 